package com.ychen.ruijee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ychen.ruijee.bean.Employee;
import com.ychen.ruijee.common.BaseContext;
import com.ychen.ruijee.common.R;
import com.ychen.ruijee.dao.EmployeeDao;
import com.ychen.ruijee.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author ychen@gotop.cn
 * @date 2024-05-28 0:00
 */
@Slf4j
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements EmployeeService {
    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {
        // 1、将页面提交的 密码 password 进行 MD5 加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 2、根据 页面提交的 用户username 去查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = this.getOne(queryWrapper);

        // 3、判断 查询的结果有没有
        if( one == null){
            return R.error("用户名或者密码错误！");
        }

        // 4、比对密码
        if(!password.equals(one.getPassword())){
            return R.error("用户名或者密码错误");
        }

        // 5、查看员工状态
        if(one.getStatus() == 0){
            return R.error("该账号已被封禁！");
        }

        // 6、登录成功 用户 id 写到 session中 供后序 业务需要
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }

    /**
     * 员工新增
     *
     * @param request
     * @param employee
     */
    @Override
    public boolean saveEmployee(HttpServletRequest request, Employee employee) {
        log.info("新增的员工信息为 ：{}",employee.toString());
        // 设置初始秘密啊
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());*/

        // 获得当前创建用户的 id
        //Long loginId = (Long) request.getSession().getAttribute("employee");
       /* employee.setCreateUser(loginId);
        employee.setUpdateUser(loginId);*/
        // 获取 ThreadLocal中的 id


        return this.save(employee);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     */
    @Override
    public R<Page<Employee>> selectPage(Integer page, Integer pageSize, String name) {
        // 构建分页器
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        // 模糊查询
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        // 排序
        wrapper.orderByDesc(Employee::getUpdateTime);
        // 执行查询
        this.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }

    /**
     * 员工状态修改
     *
     * @param request
     * @param employee
     */
    @Override
    public boolean updateStattus(HttpServletRequest request, Employee employee) {
        log.info("+++++++++>要修改的员工信息为 ：{}",employee.toString());
        //Long loginId = (Long) request.getSession().getAttribute("employee");
        // 修改该账号被修改的信息
        /*employee.setUpdateUser(loginId);
        employee.setUpdateTime(LocalDateTime.now());*/
        // 更新
        boolean flag = this.updateById(employee);
        return flag;


    }
}
